﻿using CNotebook.Sales;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using NotebookS2.Sales.Dialogs; 
using CNotebook;

namespace NotebookS2
{
    public partial class FND_INVOICE : RibbonForm
    {
        public FND_INVOICE()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string DocumentNumber { get; set; }
        public string CustomerCode { get; set; }
        public string CustmerName { get; set; }
        public string TotalBalance { get; set; }

        void BindGridByDocument(string _DocNumber)
        {
            InvoiceManager oManager = new InvoiceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.GetInvoices(_DocNumber);
            gridControl1.Refresh();

            oManager.Close();
        }


        void BindGridBySIDRNo(string _SIDRNo)
        {
            InvoiceManager oManager = new InvoiceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.GetInvoicesBySIDRNo(_SIDRNo);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByCustRefPO(string _CustRefPO)
        {
            InvoiceManager oManager = new InvoiceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.GetInvoicesByCustRefPO(_CustRefPO);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByCustomerCode(string _CODE)
        {
            InvoiceManager oManager = new InvoiceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.GetInvoicesByBranchCode(_CODE);
            gridControl1.Refresh();

            oManager.Close();
        }

        void BindGridByCustomer(string _CustomerName)
        {
            InvoiceManager oManager = new InvoiceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.GetInvoicesByBranchName(_CustomerName);
            gridControl1.Refresh();

            oManager.Close();
        }


        void BindGridByRefSONo(string _SIDRNo)
        {
            InvoiceManager oManager = new InvoiceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            gridControl1.DataSource = oManager.GetInvoicesByRefSONo(_SIDRNo);
            gridControl1.Refresh();

            oManager.Close();
        }

        private void txtBPCode_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }

        private void FND_DLVRY_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {

            int iRowIndex = gridView1.FocusedRowHandle;

            this.DocumentNumber = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["DocNum"]).ToString();
            this.CustmerName = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["CardName"]).ToString();
            this.TotalBalance = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["TotalBalance"]).ToString();

            this.Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm5));

            if (cboSearch.SelectedIndex == 0)
            {
                BindGridByDocument(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 1)
            {
                BindGridBySIDRNo(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 2)
            {
                BindGridByCustRefPO(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 3)
            {

                BindGridByCustomerCode(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 4)
            {

                BindGridByCustomer(txtSearch.Text);
            }
            else if (cboSearch.SelectedIndex == 5)
            {
                BindGridByRefSONo(txtSearch.Text);
            }

            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (cboSearch.SelectedIndex == 0)
                {
                    BindGridByDocument(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 1)
                {
                    BindGridBySIDRNo(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 2)
                {
                    BindGridByCustRefPO(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 3)
                {

                    BindGridByCustomerCode(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 4)
                {

                    BindGridByCustomer(txtSearch.Text);
                }
                else if (cboSearch.SelectedIndex == 5)
                {
                    BindGridByRefSONo(txtSearch.Text);
                }

                if (gridView1.RowCount > 0)
                {
                    gridControl1.Focus();
                }
            }
        }

        private void txtSearch_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
